using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SystemFunctionLib.Apis;

namespace SystemFunctionLib.SystemEnvironment
{
    /// <summary>
    /// Processes the system hot key message.
    /// </summary>
    internal class SystemHotKeyMessageFilter : IMessageFilter
    {
        /// <summary>
        /// Initialize a new instance of the <see cref="SystemHotKeyMessageFilter"/> with
        /// the specific <see cref="SystemHotKeyListener"/>
        /// </summary>
        /// <param name="listner">An instance of the <see cref="SystemHotKeyListener"/>.</param>
        public SystemHotKeyMessageFilter(SystemHotKeyListener listner)
        {
            if (listner == null)
            {
                throw new ArgumentNullException("listner");
            }

            _listner = listner;
        }

        private SystemHotKeyListener _listner = null;

        #region IMessageFilter Members

        public bool PreFilterMessage(ref Message m)
        { 
            switch (m.Msg)
            {
                case WinApis.WM_HOTKEY:
                    // Get the system hot key message.
                    ProcessHotkey(m);
                    break;
            }

            return false;
        }

        #endregion

        private void ProcessHotkey(Message m)
        {
            // Get internal id of the system hot key.
            int sid = m.WParam.ToInt32();

            // Check if it is registered in this program.
            if (_listner._hotKeys.ContainsKey(sid))
            {
                // If yes, invoke the event.
                HotKey hotKey = _listner._hotKeys[sid];
                _listner.InvokeSystemHotKeyPressed(hotKey);
            }
        }

    }
}
